package controllers.wechatlab;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.ansj.app.keyword.Keyword;
import org.ansj.app.summary.SummaryComputer;
import org.ansj.app.summary.pojo.Summary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.bjtu.labsys.constant.Constant;
import controllers.BaseController;
import pojo.ResCode;
import pojo.jane.AuthorityBean;
import pojo.jane.StudentBean;
import pojo.weichatlab.Label;
import pojo.weichatlab.Wechat;
import service.wechatlab.LabelService;
import service.wechatlab.LableSystemService;
import service.wechatlab.WechatService;

@Controller
@RequestMapping(value = "label")
public class LabelSystemController extends BaseController {
	@Autowired
	LableSystemService lss;
	@Autowired
	LabelService ls;
	@Autowired
	WechatService ws;

	// 注册
	@ResponseBody
	@RequestMapping(value = "/register.do", method = RequestMethod.POST)
	public String register(StudentBean student, HttpServletRequest request)
			throws NoSuchAlgorithmException, IOException {
		if (lss.isLegal(student.getUserid())) {
			if (lss.addUserService(student) > 0) {
				System.out.println(student.getAuthgroup());
				HttpSession session = request.getSession();
				// 将数据存储到session中
				session.setAttribute("userid", student.getUserid());
				session.setAttribute("name", student.getName());
				session.setAttribute("group", student.getAuthgroup());
				session.setAttribute("auth", lss.authInfoMap(student.getUserid()));				
				return jsonDump(SUCCESS);
			} else {
				return jsonDump(new ResCode(400, "Fail"));
			}
		} else {
			return jsonDump(new ResCode(400, "UserId Illegal"));
		}
	}

	// 登录
	@ResponseBody
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public String login(StudentBean student, HttpServletRequest request)
			throws NoSuchAlgorithmException, IOException {
		// TODO Auto-generated method stub
		if (lss.isExistUser(student)) {
			HttpSession session = request.getSession();
			// 将数据存储到session中
			StudentBean st = lss.selectStudent(student.getUserid()).get(0);
			session.setAttribute("userid", student.getUserid());
			session.setAttribute("name", st.getName());
			session.setAttribute("group", st.getAuthgroup());
			session.setAttribute("auth", lss.authInfoMap(student.getUserid()));		
			return jsonDump(SUCCESS);
		} else {
			return jsonDump(ParamsError);
		}
	}

	// 设置标签，此项是设置所有文本可选的类标，不是给具体某个文本打标签
	@ResponseBody
	@RequestMapping(value = "/setavailablelabel.do")
	public String setAvailableLable(Label label) {
		return "";
	}
	
	
	// 选择待标注文本，返回文本url、推荐类标、以前的标注结果等信息
	@ResponseBody
	@RequestMapping(value = "/gettext.do")
	public ModelAndView getText(HttpServletRequest request)  throws Exception {
		//获得所有类标
		List<Label> labellist = ls.selectAllParentLabels();
		//得到微信文本
		List<Wechat> wechatlist = lss.getText(request);
		
		if (wechatlist.size() > 0){
			Wechat wechat = wechatlist.get(0);
			//获取关键词
			List<String> keywords = ls.getKkeyWords(wechat.getTitle(), wechat.getContent(), 5);
			return new ModelAndView("label", "classes", labellist).addObject("wechat", wechat).addObject("keyWords", keywords);
		}
		else
			return new ModelAndView("label", "classes", labellist);
	}
	
	@RequestMapping("/getAllChildren.do")
	@ResponseBody
	public String ajaxLoadAllChildClasses(HttpServletRequest req,HttpServletResponse res){
		List<Label> childList = ls.selectAllChildLabels();
		return jsonDump(childList);	
	}

	// 为文本添加标签，此项是为某个文本添加标签
	@ResponseBody
	@RequestMapping(value = "/settextlabel.do")
	public String setTextLable(HttpServletRequest request) {
		return jsonDump(lss.updateTextLable(request));
		// return new ModelAndView("label");
	}
	
	@ResponseBody
	@RequestMapping(value="/logout")
	public void logout(HttpServletRequest req, HttpServletResponse res){
		HttpSession session = req.getSession(false);
		if(session != null){
			session.removeAttribute("auth");
			session.removeAttribute("userid");
			session.removeAttribute("name");
			session.removeAttribute("group");
		}
	}

}